package com.tianma315.web.code_flag.controller;


import java.util.Arrays;

import com.tianma315.core.code_flag.domain.CodeFlagDO;
import com.tianma315.core.code_flag.service.CodeFlagService;
import com.tianma315.core.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


/**
 * 
 * <pre>
 * 查询记录
 * </pre>
 * <small> 2020-02-26 17:10:59 | Aron</small>
 */
@Controller
@RequestMapping("/code_flag/codeFlag")
public class CodeFlagController {
	@Autowired
	private CodeFlagService codeFlagService;
	
	@GetMapping()
	@RequiresPermissions("code_flag:codeFlag:codeFlag")
	String CodeFlag(){
	    return "code_flag/codeFlag/codeFlag";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("code_flag:codeFlag:codeFlag")
	public Result<Page<CodeFlagDO>> list(Integer pageNumber, Integer pageSize, CodeFlagDO codeFlagDTO){
		// 查询列表数据
        Page<CodeFlagDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<CodeFlagDO> wrapper = new EntityWrapper<CodeFlagDO>(codeFlagDTO);
        page = codeFlagService.selectPage(page, wrapper);
        int total = codeFlagService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("code_flag:codeFlag:add")
	String add(){
	    return "code_flag/codeFlag/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("code_flag:codeFlag:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CodeFlagDO codeFlag = codeFlagService.selectById(id);
		model.addAttribute("codeFlag", codeFlag);
	    return "code_flag/codeFlag/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("code_flag:codeFlag:add")
	public Result<String> save( CodeFlagDO codeFlag){
		if(codeFlagService.insert(codeFlag)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("code_flag:codeFlag:edit")
	public Result<String>  update( CodeFlagDO codeFlag){
		codeFlagService.updateById(codeFlag);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("code_flag:codeFlag:remove")
	public Result<String>  remove( Long id){
		if(codeFlagService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("code_flag:codeFlag:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		codeFlagService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
